var test = 500000;
$().ready(function() {
	validateRule();
	initDictDatas();

});

//构建字典数据
function initDictDatas(){

	//企业
	$('#enterpriseId').bootstrapSelect({
		url:ctx+'plat/enterprise/getAll',
		valueField: 'id',
		textField: 'enterpriseName',
		placeholder: "请选择服务企业",
	});
	$('#enterpriseId').selectpicker('refresh');
	// $('#enterpriseId').bootstrapSelect('setValue',enterpriseId);
	// $("#enterpriseId").next().next().css("display","none");

}


// $.validator.setDefaults({
// 	submitHandler : function() {
// 		save();
// 	}
// });

// function saveMyForm() {
// 	console.log('aaa');
// }

function saveMyForm() {
	$.ajax({
		cache : false,
		type : "POST",
		url : ctx + "plat/roster/save",
		data : {
			rosterName: $('#rosterName').val(),
			enterpriseId: $('#enterpriseId').val(),
			enterpriseContactsName: $('#enterpriseContactsName').val(),
			enterpriseContactsPhone: $('#enterpriseContactsPhone').val(),
			child: JSON.stringify($('#table').bootstrapTable('getData'))
		},
		// $('#signupForm').serialize(),// 你的formid
		async : false,

		success : function(data) {
			if (data.code == 0) {
				parent.layer.msg("操作成功");
				parent.reLoad();
				var index = parent.layer.getFrameIndex(window.name); // 获取窗口索引
				parent.layer.close(index);

			} else if(data.code == 503) {
				parent.layer.alert(data.msg);
				returnData = data;
				parent.layer.alert(data.roster.rosterName);
					$('#enterpriseId').val(data.roster.enterpriseId);
					$('#enterpriseContactsName').val(data.roster.enterpriseContactsName);
					$('#enterpriseContactsPhone').val(data.roster.enterpriseContactsPhone);
					$('#table').bootstrapTable('removeAll');
					var list = JSON.parse(data.roster.child);
				for(j = list.length - 1; j >= 0; j--) {
					// alert(list[j].rosterDetailId);
					$('#table').bootstrapTable('insertRow', {
						index: 0,
						row: {
							rosterDetailId: list[j].rosterDetailId,
							// id: '',
							userName: list[j].userName,
							phone: list[j].phone,
							errorCause: list[j].errorCause
						}
					});
				}
			}else {
				parent.layer.msg(data.msg);
			}
		}
	});

}


function validateRule() {
	var icon = "<i class='fa fa-times-circle'></i> ";
	$("#signupForm").validate({
		rules : {
			rosterName : {
				required : true
			},

			enterpriseId : {
				required : true,
				maxlength:100,
				minlength:5,
			}
		},
		messages : {
			rosterName : {
				required : icon + "请输入企业名称"
			},

			enterpriseId : {
				required : icon + "请输入地址",
				maxlength: icon + "地址长度最大为100个字符",
				minlength: icon + "地址长度最小为5个字符",

			}

		}
	})
}


function addEnterprise() {
	var width = $(window).width()*0.5 + 'px';
	var height = $(window).height()*0.6 + 'px';
	layer.open({
		type : 2,
		title : '添加企业',
		maxmin : true,
		shadeClose : false, // 点击遮罩关闭层
		area : [ width, height ],
		content :  ctx + 'plat/roster/addEnterprise' // iframe的url
	});
}


function checkEnterpriseContacts() {
	var enterpriseId =  $('#enterpriseId').val();
	if (enterpriseId == null || enterpriseId == ''){
		layer.msg("请选择服务企业");
		return;
	}
	var width = $(window).width()*0.5 + 'px';
	var height = $(window).height()*0.6 + 'px';
	layer.open({
		type : 2,
		title : '选择企业联系人',
		maxmin : true,
		shadeClose : false, // 点击遮罩关闭层
		area : [ width, height ],
		content :  ctx + 'plat/roster/enterpriseContacts/'+enterpriseId // iframe的url
	});
}

//动态table编辑页
$(function() {
	let $table = $('#table');
	let $button = $('#button');
	let $getTableData = $('#getTableData');

	$button.click(function() {
		var indexNum;
		var child = $table.bootstrapTable('getData');
		if (child.length == 0) {
			indexNum = 0;
		}else {
			indexNum = child.length;
		}
		// alert(indexNum);
		test = test + 1 ;
		$table.bootstrapTable('insertRow', {
			index: indexNum,
			row: {
				rosterDetailId: test,
				userName: '',
				phone: '',
				errorCause:''
			}
		});
		num = num +1;
		document.getElementById('num').innerHTML=num;
	});

	$table.bootstrapTable({
		// url: 'data2.json',
		// url: '',
		toolbar: '#toolbar',
		clickEdit: true,		//点击修改
		showToggle: false,      //显示切换列名显示与隐藏
		pagination: true,       //显示分页条
		showColumns: false,		//显示展示列的调节功能
		showPaginationSwitch: false,     //显示切换分页按钮
		showRefresh: false,      //显示刷新按钮

		columns: [

			{
				field: 'rosterDetailId',
				title: '序号',
				formatter: function (value, row, index) {
					return index + 1;
				}
			},
			{
				field: 'userName',
				title: '姓名',
				width:300,
				align : 'center'
			}, {
				field: 'phone',
				title: '手机号',
				width:300,
				align : 'center',
				min:0
			},{
				field: 'errorCause',
				title: '校验结果',
				width:300,
				align : 'center',
				formatter : function(value, row, index) {
						var a = "";
						if(value == "校验通过") {
							a = '<span style="color:#00ff00">'+'校验通过'+'</span>';
						}else if(value == "手机号格式不正确"){
							a = '<span style="color:#FF0000">'+'手机号格式不正确'+'</span>';
						}else if(value == "手机号系统中不存在，需要注册") {
							a = '<span style="color:#FF0000">'+'手机号系统中不存在，需要注册'+'</span>';
						}else if(value == "手机号已存在于此名单") {
							a = '<span style="color:#FF0000">'+'手机号已存在于此名单'+'</span>';
						}else if(value == "姓名不能超过20个字") {
							a = '<span style="color:#FF0000">'+'姓名不能超过20个字'+'</span>';
						}else{
							a = '<span style="color:white">'+''+'</span>';
						}
						return a;
				}
			},

			{
				field:'acc',
				title:'操作',
				width:300,
				align : 'center',
				formatter : function(value, row, index) {
					var d = '<a  href="#" title="删除"  mce_href="#" onclick="removeTab(\''
						+ row.rosterDetailId
						+ '\')"><i >删除</i></a> ';
					return d ;
				}
			}
		],
		/**
		 * @param {点击列的 field 名称} field
		 * @param {点击列的 value 值} value
		 * @param {点击列的整行数据} row
		 * @param {td 元素} $element
		 */
		onClickCell: function(field, value, row, $element) {

			if (field == 'rosterDetailId' || field == 'acc' || field == 'errorCause'){
				return;
			}
			$element.attr('contenteditable', true);
			$element.focus();

			$element.blur(function() {
				let index = $element.parent().data('index');
				let tdValue = $element.html();


				saveData(index, field, tdValue);
			})
		}
	});

	$getTableData.click(function() {
		var child = $table.bootstrapTable('getData');
		var man = $('#signupForm').serialize();
		alert("主体"+JSON.stringify(man) +"子集"+ JSON.stringify(child));
	});

	function saveData(index, field, value) {
		$table.bootstrapTable('updateCell', {
			index: index,       //行索引
			field: field,       //列名
			value: value        //cell值
		});
	}



});
//table 到此结束

function removeTab(rosterDetailId) {

			// alert(rosterDetailId);
			$('#table').bootstrapTable('remove', {
				field: 'rosterDetailId',
				values: [parseInt(rosterDetailId)],       //行索引
			});
			num = num - 1;
			document.getElementById('num').innerHTML = num;

}


function closeMyForm() {
	var index = parent.layer.getFrameIndex(window.name); // 获取窗口索引
	parent.layer.close(index);
}


